﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;
using TyMES.Model;
using TyMES.ViewModel.Record.ProduceDataRecordVMs;
using System.Threading.Tasks;
using TyMES.Services;
using NetBox.Extensions;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;


namespace TyMES.Record.Controllers
{
    [AuthorizeJwtWithCookie]
    public partial class ProduceDataRecordController : BaseApiController
    {
                                                
        [ActionDescription("Sys.Search")]
        [HttpPost("[action]")]
        public IActionResult SearchProduceDataRecord(TyMES.ViewModel.Record.ProduceDataRecordVMs.ProduceDataRecordSearcher searcher)
        {
            if (ModelState.IsValid)
            {
                var vm = Wtm.CreateVM<TyMES.ViewModel.Record.ProduceDataRecordVMs.ProduceDataRecordListVM>();
                vm.Searcher = searcher;
                return Content(vm.GetJson());
            }
            else
            {
                return BadRequest(ModelState.GetErrorJson());
            }
        }

        [ActionDescription("Sys.Export")]
        [HttpPost("[action]")]
        public IActionResult ProduceDataRecordExportExcel(TyMES.ViewModel.Record.ProduceDataRecordVMs.ProduceDataRecordSearcher searcher)
        {
            var vm = Wtm.CreateVM<TyMES.ViewModel.Record.ProduceDataRecordVMs.ProduceDataRecordListVM>();
            vm.Searcher = searcher;
            vm.SearcherMode = ListVMSearchModeEnum.Export;
            return vm.GetExportData();
        }

        [ActionDescription("Sys.CheckExport")]
        [HttpPost("[action]")]
        public IActionResult ProduceDataRecordExportExcelByIds(string[] ids)
        {
            var vm = Wtm.CreateVM<TyMES.ViewModel.Record.ProduceDataRecordVMs.ProduceDataRecordListVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = new List<string>(ids);
                vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
            }
            return vm.GetExportData();
        }


        private readonly SPCService spcService;

		public ProduceDataRecordController(SPCService spcService)
		{
			this.spcService = spcService;
		}

		public record SPCAnalysisPara(string lineCode, string stationCode, int order, int len, int num, int type);
        [HttpPost("SPCAnalysis")]
        public IActionResult SPCAnalysis(SPCAnalysisPara para)
        {
			try
			{
				var res = spcService.Analysis(para.order, para.num, para.len, para.stationCode, para.type);
                return Ok(new { Code = "00", Dtat=res, Mesg = "success"});
			}
			catch (Exception ex)
			{

                return Ok(new { Code = "01", Mesg = "SPC分析过程发生异常" });
            }
        }
    
    }
}


